package com.gwt.boot.model;


import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.util.List;

/**
 * 分页查询基础对象
 */
@Data
public class PageQuery {
    @JsonIgnore
    private final Long MAX_ROW = 1000L;

    /**
     * 设置当前页
     */
    private Long current = 1L;

    /**
     * 设置每页显示条数,默认10条
     */
    private Long size = 10L;

    /**
     * 是否分页,默认：true
     */
    private boolean isPage = true;

    /**
     * 全局模糊匹配
     */
    private String vague;

    @JsonIgnore
    private PageEntity page;

    public <T> PageEntity<T> getPage() {
        if (page == null) {
            if (!isPage) {
                page = new PageEntity<>(current, MAX_ROW);
            }else {
                page = new PageEntity<>(current, size);
            }
        }
        return page;
    }

    public <T> PageResult<T> getResult(List<T> list) {
        if (list == null || list.isEmpty()) {
            return new PageResult<>(null, 0L);
        }
        if (!isPage || page == null) {
            return new PageResult<>(list, list.size());
        }
        return new PageResult<>(list, page.getTotal());
    }

    public boolean isPage() {
        return isPage;
    }
}